#include "initAnts.h"
#include <stdio.h>


char easy_plan_dir(int start_row, int start_col, int target_row, int target_col, struct game_info *Info){
    char plan_char = -1;
    char plan_char_row = -1;
    char plan_char_col = -1;
    // nejdrive jednoduchy pohyb
    int d_row = start_row - target_row;
    int d_col = start_col - target_col;
    // zjisteni zda cesta opacnym smerem neni blizsi
    if ( abs(d_row) > Info->rows / 2 ) {
        if (d_row < 0) d_row += Info->rows ; 
        else d_row -= Info->rows;
    }
    if ( abs(d_col) > Info->cols / 2 )
        if (d_col < 0) d_col += Info->cols;
        else d_col -= Info->cols;
    // nejdrive se dam smerem, ktery je vic vzdaleny
    if (d_row > 0) plan_char_row = 'N';
    else plan_char_row = 'S';
    if (d_col > 0) plan_char_col = 'W';
    else plan_char_col = 'E';
    // nyni rozhodnu, zda se mohu pohnout

    if (can_ant_move(start_row, start_col, plan_char_row, Info) &&
            can_ant_move(start_row, start_col, plan_char_col, Info))
        if (abs(d_row) > abs(d_col) )
            plan_char = plan_char_row;
        else
            plan_char = plan_char_col;
    else
        if (can_ant_move(start_row, start_col, plan_char_row, Info))
            plan_char = plan_char_row;
        else if (can_ant_move(start_row, start_col, plan_char_col, Info))
            plan_char = plan_char_col;
    // else nemuzu se pohnout ani jednim smerem
    // vratim -1... musim planovat cestu.

    return plan_char;

}

char plan_move(struct my_ant *ant, struct food *target_food, struct game_info *Info){
    return easy_plan_dir(ant->row, ant->col, target_food->row, target_food->col, Info);
}


char right_sidewalk(char direction){
    // to jakoze se drzi napravo...
    switch (direction) {
        case 'N':
            return 'E';
        case 'E':
            return 'S';
        case 'S':
            return 'W';
        case 'W':
            return 'N';
        default:
            return 'N';
    }
}

// funkce, ktera vyplni my_ant.movement
void plan_movement (struct my_ant *ant, struct food *target_food, struct game_info *Info){
    char * p_move = plan_movement_aStar(ant->row, ant->col, target_food->row, target_food->col, Info);
    int len_p_move = strlen(p_move);
    if (len_p_move < MOV_LEN) {
        strncpy(ant->movement, p_move, len_p_move);
        ant->movement[len_p_move] = '\0';
    } else {
        strncpy(ant->movement, p_move, MOV_LEN);
        ant->movement[MOV_LEN - 1] = '\0';
    }
    // protoze je to jidlo, staci byt vedle
//    if ((len_p_move > 1) && (len_p_move <= MOV_LEN))
//        ant->movement[len_p_move - 1] = '\0';
//    else
//        ant->movement[MOV_LEN - 1] = '\0';
    free(p_move);
}

char get_map_char(int row, int col, struct game_info * Info) {
    return Info->map[ Info->cols*row + col ];
}

// udela posun a ulozi preulozi movement
void exec_movement ( int m_i, struct my_ant *ant, struct game_state * Game, struct game_info *Info){
    int len_mov = strlen(ant->movement);
    if (len_mov > 0){
        if ( can_ant_move(ant->row, ant->col, ant->movement[0], Info)){
            move(m_i, ant->movement[0], Game, Info);
            if (len_mov > 1) {
                strncpy(ant->movement, & ant->movement[1], MOV_LEN - 1);
                ant->movement[len_mov - 1]='\0';
            } else {
                ant->movement[0]='\0';
                ant->order=-1;
                plan_ant_order(m_i, Game, Info);
            }
        } else {
            ant->movement[0]='\0';
            ant->order=-1;
            plan_ant_order(m_i, Game, Info);
        }
    }

}

// navrati index nejblizsiho jidla
int nearest_food_index(int m_i, struct game_state *Game, struct game_info *Info){
    int nearest_f_i = -1;
    int nearest_food_dist = (Info->cols * Info->rows) / 2;
    int temp_dist = 0;
    int f_i = 0;
    for (; f_i < Game->food_count; f_i++){
        temp_dist = steps_distance(Game->my_ants[m_i].row, Game->my_ants[m_i].col,
                        Game->food[f_i].row, Game->food[f_i].col, Info);
        if ( nearest_food_dist > temp_dist ){
            nearest_food_dist = temp_dist;
            nearest_f_i = f_i;
        }
    }
    return nearest_f_i;
}

void plan_ant_order(int m_i, struct game_state *Game, struct game_info *Info) {
    int f_i = -1;
    int test_int = 0;
    // zbyva hledat jidlo... muj order je F .. food
    f_i=nearest_food_index(m_i,Game,Info);
    test_int = steps_distance(Game->my_ants[m_i].row, Game->my_ants[m_i].row,
                Game->food[f_i].row, Game->food[f_i].row, Info);
    if ( (f_i >= 0) && 
            ((Game->food[f_i].a_i < 0) || (Game->food[f_i].a_i == m_i)) &&
        //    (test_int <= 2*(int)sqrt(Info->viewradius_sq) ) &&
            (test_int <= Info->viewradius_sq ) &&
            (test_int > (int)sqrt(Info->spawnradius_sq) ) ) {
        // je nejaky blizky food, 
        // nikdo pro nej nejde, 
        // a je bliz nez co vidim
        plan_movement( & Game->my_ants[m_i], & Game->food[f_i], Info);
        // protoze je to jidlo, muzu zkratit movement o 1 char
        test_int = strlen( Game->my_ants[m_i].movement );
        if (test_int > 1)
            Game->my_ants[m_i].movement[ test_int - 1 ] = '\0';
        Game->my_ants[m_i].order='F';
        Game->my_ants[m_i].t_row=Game->food[f_i].row;
        Game->my_ants[m_i].t_col=Game->food[f_i].col;
        Game->food[f_i].a_i=m_i;
        //debug_log(Game->my_ants[m_i].movement);
    }
}

void do_turn(struct game_state *Game, struct game_info *Info) {

    // defining things just so we can do less writing
    // UP and DOWN move up and down rows while LEFT and RIGHT
    // move side to side. The map is just one big array.

    #define UP -Info->cols
    #define DOWN Info->cols
    #define LEFT -1
    #define RIGHT 1

    int m_i;
    int h_i;
    int f_i;
    int i;

    // globalni rozhodnuti vsech pravencu
    // - interup (prerusit vsechny svoje plany a preplanovat
    // - scouting food
    // - defend hill
    // - attacking enemy hill


    // ve forcyklu budu rozhodovat jako jednotlivec - mravence
    // zajem cislo jedna, najit jidlo
    //
    // nejprve se zkontroluji cile mravencu, odpovida-li cil jidlu nastavi
    // se do food.a_i, index na mravence, ktery v tomto kole jde na jidlo

    // kontrola jidla...
    // TODO chybi mi kontrola, ze cestou jsem nenarazil na blizsi jidlo...
    for (m_i = 0; m_i < Game->my_count; m_i++)
        if (Game->my_ants[m_i].order == 'F')
            for (f_i = 0; f_i < Game->food_count; f_i++)
                if (( Game->my_ants[m_i].t_row == Game->food[f_i].row) &&
                        (Game->my_ants[m_i].t_col == Game->food[f_i].col)){
                    Game->food[f_i].a_i = m_i;
                    break;
                }

    for (m_i = 0; m_i < Game->my_count; m_i++) {
        if (Game->my_ants[m_i].order == -1)
            plan_ant_order(m_i, Game, Info);
        // provedeni tahu mravence...
        exec_movement(m_i, & Game->my_ants[m_i], Game, Info);
    }

}

